package top.xana.acg.anime.ui.page

import com.google.android.material.tabs.TabLayoutMediator
import top.xana.acg.anime.R
import top.xana.acg.anime.databinding.MainFragmentBinding
import top.xana.acg.base.BaseFragment
import top.xana.acg.base.ViewPager2Adapter

class MainFragment : BaseFragment<MainFragmentBinding>(R.layout.main_fragment) {

    override fun initView() {
        super.initView()
        binding.inputView.setOnClickListener {
            navTo(R.id.toPageSearch)
        }
        setTabViewPager()
    }

    private fun setTabViewPager() = with(binding) {
        val arr = resources.getStringArray(R.array.mainTab)
        viewPager2.adapter = ViewPager2Adapter(
            arr.mapIndexed { i, _ ->
                if (i > 0) CategoryFragment.newInstance(i - 1)
                else HomeFragment.newInstance()
            },
            childFragmentManager,
            lifecycle
        )
        TabLayoutMediator(tabLayout, viewPager2) { tab, index ->
            tab.text = arr[index]
        }.attach()
    }

}